/**
 * Created by 功振 on 2016/2/2.
 */
angular.module('NatureCloud').controller('BuildCreateController', ["$scope","$state","$uibModalInstance", "$cookies", "buildAddition", "projectListService", "utilService","$mdDialog",
    function ($scope,$state,$uibModalInstance, $cookies, buildAddition, projectListService, utilService,$mdDialog) {
        $scope.operation = "新建";
        $scope.isCodeRepoListShow = true;
        $scope.buildInfo = {};
        $scope.buildnameReg = '[a-z0-9]+(?:[._-][a-z0-9]+)*';

        $scope.ok = function () {
            if($scope.form.$valid) {
                var user = $cookies.get("username");
                var buildInfo = {
                    id: $scope.buildInfo.id,
                    gitPath: $scope.gitPath,
                    //user: $scope.user,
                    user: user,
                    buildContext: $scope.buildInfo.buildContext === undefined ? '' : $scope.buildInfo.buildContext,
                    repoName: $scope.buildInfo.id,
                    // isCI:$scope.buildInfo.isCI

                    isCI:$scope.buildInfo.isCI === undefined ? false : $scope.buildInfo.isCI
                };

                utilService.copyProperties($scope.selectedRepoInfo, buildInfo);
                buildInfo.id = $scope.buildInfo.id;
                buildInfo.type = $scope.repoType;

                // console.log("clone_url:" + buildInfo.clone_url + ", buildContext:" + buildInfo.buildContext + ", repoType:" + buildInfo.type);

                buildAddition(angular.toJson(buildInfo, true)).then(function (r) {
                    // alert success

                    var resut = r.data;
                    var  data = resut.data;

                    var urls = window.location.host;
                    var path = data.gitPath;
                    var type;
                    var gittype = isContains(path,"github.com");
                    if (gittype === true){
                        type = "github";
                    }
                    var bittype = isContains(path,"bitbucket.org");
                    if (bittype === true){
                        type = "bitbucket"
                    }
                    var _webhook = "请将该url填入到" + type + "上Webhook的URL中:" +"http://" + urls +"/opBuildInfo/webhook/" + data._id;

                    if(data.isCI === true){
                        alertify.alert("请手动添加webhook URL",_webhook,function () {

                        });
                    }

                    alertify.success(buildInfo.id+" build addition succeed");

                    $uibModalInstance.close();
                    $state.go("cp.buildlist");
                }, function (r) {
                    // alert fail
                    alertify.error(buildInfo.id + " build addition failed");
                    $uibModalInstance.close();

                });

            }
        };

        function isContains(str,substr) {
            return str.indexOf(substr) >= 0;
        }

        $scope.cancel = function () {
            $mdDialog.cancel();
        };
    }
]);